package com.wgts.wgts.control.refresh;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.AnimationDrawable;
import android.media.Image;
import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshKernel;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.header.BezierRadarHeader;
import com.scwang.smartrefresh.layout.header.bezierradar.RippleView;
import com.scwang.smartrefresh.layout.header.bezierradar.RoundDotView;
import com.scwang.smartrefresh.layout.header.bezierradar.RoundProgressView;
import com.scwang.smartrefresh.layout.header.bezierradar.WaveView;
import com.scwang.smartrefresh.layout.internal.ProgressDrawable;
import com.scwang.smartrefresh.layout.internal.pathview.PathsView;
import com.scwang.smartrefresh.layout.util.DensityUtil;

import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;

/*
 *
 *
 *   data:2018/9/1116:31
 *
 */
public class ClassicsHeader extends LinearLayout implements RefreshHeader {
    private boolean mEnableHorizontalDrag = false;

    private ImageView startImage;
    private ImageView loadImage;
    private AnimationDrawable startDrawable, loadDrawable;
    public ClassicsHeader(Context context) {
        this(context,null);
    }

    public ClassicsHeader(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ClassicsHeader(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs, defStyleAttr);
    }

    private void initView(Context context, AttributeSet attrs, int defStyleAttr) {
        View view = LayoutInflater.from(context).inflate(R.layout.refresh_head_view, null);
        startImage = view.findViewById(R.id.start_image);
        loadImage = view.findViewById(R.id.load_image);
        //startDrawable = (AnimationDrawable) startImage.getBackground();
        loadDrawable = (AnimationDrawable) loadImage.getBackground();
        addView(view, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    }

    public ClassicsHeader setPrimaryColor(@ColorInt int color) {
        return this;
    }

    public ClassicsHeader setAccentColor(@ColorInt int color) {
        return this;
    }

    public ClassicsHeader setPrimaryColorId(@ColorRes int colorId) {
        setPrimaryColor(ContextCompat.getColor(getContext(), colorId));
        return this;
    }

    public ClassicsHeader setAccentColorId(@ColorRes int colorId) {
        setAccentColor(ContextCompat.getColor(getContext(), colorId));
        return this;
    }

    public ClassicsHeader setEnableHorizontalDrag(boolean enable) {
        this.mEnableHorizontalDrag = enable;
//        if (!enable) {
//            mWaveView.setWaveOffsetX(-1);
//        }
        return this;
    }

    //</editor-fold>

    //<editor-fold desc="RefreshHeader">
    @Override@Deprecated
    public void setPrimaryColors(@ColorInt int ... colors) {
        if (colors.length > 0) {
            setPrimaryColor(colors[0]);
        }
        if (colors.length > 1) {
            setAccentColor(colors[1]);
        }
    }

    @NonNull
    public View getView() {
        return this;
    }

    @Override
    public SpinnerStyle getSpinnerStyle() {
        return SpinnerStyle.Scale;
    }

    @Override
    public void onInitialized(RefreshKernel kernel, int height, int extendHeight) {
    }

    @Override
    public boolean isSupportHorizontalDrag() {
        return mEnableHorizontalDrag;
    }

    @Override
    public void onHorizontalDrag(float percentX, int offsetX, int offsetMax) {

    }

    @Override
    public void onPullingDown(float percent, int offset, int headerHeight, int extendHeight) {
        String a = "================================================Percent>";
        String b = "================================================offset>";
        String c = "================================================headerHeighr>";
        String d = "================================================extendHeight>";
        Log.d( "", a + percent);
        Log.d("", b + offset);
        Log.d("", c + headerHeight);
        Log.d("", d + extendHeight);
    }

    @Override
    public void onReleasing(float percent, int offset, int headerHeight, int extendHeight) {
        onPullingDown(percent, offset, headerHeight, extendHeight);
    }

    @Override
    public void onRefreshReleased(final RefreshLayout layout, int headerHeight, int extendHeight) {

    }

    @Override
    public void onStartAnimator(RefreshLayout layout, int headerHeight, int extendHeight) {

    }

    @Override
    public int onFinish(RefreshLayout layout, boolean success) {
        //startDrawable.stop();
        loadDrawable.stop();
        return 100;
    }

    @Override
    public void onStateChanged(RefreshLayout refreshLayout, RefreshState oldState, RefreshState newState) {
        switch (newState) {
            case None:
                startImage.setVisibility(VISIBLE);
                loadImage.setVisibility(GONE);
                //startDrawable.stop();
                loadDrawable.stop();
                break;
            case PullDownToRefresh:
                startImage.setVisibility(VISIBLE);
                loadImage.setVisibility(GONE);
                //startDrawable.start();
                loadDrawable.stop();
                break;
            case PullToUpLoad:
                break;
            case Refreshing:
                startImage.setVisibility(GONE);
                loadImage.setVisibility(VISIBLE);
                //startDrawable.stop();
                 loadDrawable.start();
                break;
            case Loading:
                break;
        }
    }
}

//    private TextView mHeaderText;//标题文本
//    private PathsView mArrowView;//下拉箭头
//    private ImageView mProgressView;//刷新动画视图
//    private ProgressDrawable mProgressDrawable;//刷新动画
//    private ImageView startImage;
//    private ImageView loadImage;
//    private AnimationDrawable startDrawable, loadDrawable;
//
//    public ClassicsHeader(Context context) {
//        super(context);
//        initView(context);
//    }
//    public ClassicsHeader(Context context, AttributeSet attrs) {
//        super(context, attrs);
//        this.initView(context);
//    }
//    public ClassicsHeader(Context context, AttributeSet attrs, int defStyleAttr) {
//        super(context, attrs, defStyleAttr);
//        this.initView(context);
//    }
//    private void initView(Context context) {
//        View view = LayoutInflater.from(context).inflate(R.layout.refresh_head_view, null);
//        startImage = view.findViewById(R.id.start_image);
//        loadImage = view.findViewById(R.id.load_image);
//        startDrawable = (AnimationDrawable) startImage.getBackground();
//        loadDrawable = (AnimationDrawable) loadImage.getBackground();
//        addView(view, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
//        /*setGravity(Gravity.CENTER);
//        mHeaderText = new TextView(context);
//        mProgressDrawable = new ProgressDrawable();
//        mArrowView = new PathsView(context);
//        mProgressView = new ImageView(context);
//        mProgressView.setImageDrawable(mProgressDrawable);
//        mArrowView.parserPaths("M20,12l-1.41,-1.41L13,16.17V4h-2v12.17l-5.58,-5.59L4,12l8,8 8,-8z");
//        addView(mProgressView, DensityUtil.dp2px(20), DensityUtil.dp2px(20));
//        addView(mArrowView, DensityUtil.dp2px(20), DensityUtil.dp2px(20));
//        addView(new View(context), DensityUtil.dp2px(20), DensityUtil.dp2px(20));
//        addView(mHeaderText, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);*/
//        //setMinimumHeight(DensityUtil.dp2px(60));
//    }
//    @NonNull
//    public View getView() {
//        return this;//真实的视图就是自己，不能返回null
//    }
//    @Override
//    public SpinnerStyle getSpinnerStyle() {
//        return SpinnerStyle.Scale;//指定为平移，不能null
//    }
//    @Override
//    public void onStartAnimator(RefreshLayout layout, int headHeight, int maxDragHeight) {
//        startImage.setVisibility(VISIBLE);
//        loadImage.setVisibility(GONE);
//        startDrawable.start();
//        loadDrawable.stop();
////        mProgressDrawable.start();//开始动画
//    }
//    @Override
//    public int onFinish(RefreshLayout layout, boolean success) {
//        startImage.setVisibility(VISIBLE);
//        loadImage.setVisibility(GONE);
//        startDrawable.stop();
//        loadDrawable.stop();
//        mProgressDrawable.stop();//停止动画
////        if (success){
////            mHeaderText.setText("刷新完成");
////        } else {
////            mHeaderText.setText("刷新失败");
////        }
//        return 500;//延迟500毫秒之后再弹回
//    }
//    @Override
//    public void onStateChanged(RefreshLayout refreshLayout, RefreshState oldState, RefreshState newState) {
//        switch (newState) {
//            case None:
//            case PullDownToRefresh:
//                startImage.setVisibility(VISIBLE);
//                loadImage.setVisibility(GONE);
//                startDrawable.start();
////                mHeaderText.setText("下拉开始刷新");
////                mArrowView.setVisibility(VISIBLE);//显示下拉箭头
////                mProgressView.setVisibility(GONE);//隐藏动画
////                mArrowView.animate().rotation(0);//还原箭头方向
//                break;
//            case Refreshing:
//                startImage.setVisibility(GONE);
//                loadImage.setVisibility(VISIBLE);
//                startDrawable.stop();
//                loadDrawable.start();
//                mHeaderText.setText("正在刷新");
//                mProgressView.setVisibility(VISIBLE);//显示加载动画
//                mArrowView.setVisibility(GONE);//隐藏箭头
//                break;
//            case ReleaseToRefresh:
//                mHeaderText.setText("释放立即刷新");
//                mArrowView.animate().rotation(180);//显示箭头改为朝上
//                break;
//        }
//    }
//    @Override
//    public boolean isSupportHorizontalDrag() {
//        return false;
//    }
//    @Override
//    public void onInitialized(RefreshKernel kernel, int height, int maxDragHeight) {
//    }
//    @Override
//    public void onHorizontalDrag(float percentX, int offsetX, int offsetMax) {
//    }
//    @Override
//    public void onPulling(float percent, int offset, int headHeight, int maxDragHeight) {
//    }
//    @Override
//    public void onReleasing(float percent, int offset, int headHeight, int maxDragHeight) {
//    }
//    @Override
//    public void onRefreshReleased(RefreshLayout layout, int headerHeight, int maxDragHeight) {
//    }
//    @Override
//    public void setPrimaryColors(@ColorInt int ... colors){
//    }
//}
